package com.kanshu.ksgb.fastread.doudou.module.book.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseListFragment;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.PageRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.BookListReqParams;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.RankingCollectAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.RankingFinishedAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.RankingRenqiAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;

/**
 * Created by wcy on 2017\7\11 0011.
 */

public class RankingListFragment extends BaseListFragment<BookInfo> implements OnRecyclerViewItemClickListener, IChangeSexListener {

    public static final String EXTRA_RANKINGLIST_PARAMS = "ranklist_type";
    public static final String EXTRA_RANKINGLIST_SITE = "ranklist_site";
    public static final String TYPE_RANKINGLIST_RENQI = BookListReqParams.TYPE_CLICK;
    public static final String TYPE_RANKINGLIST_COLLECT = BookListReqParams.TYPE_COLLECT;
    public static final String TYPE_RANKINGLIST_FINISH = BookListReqParams.TYPE_FINISHED;
    private BookCityPresenter mPresenter = new BookCityPresenter(lifeCyclerSubject);
    private String mType;
    private int mSite;

    public static RankingListFragment newInstance(String type, int site) {
        RankingListFragment fragment = new RankingListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_RANKINGLIST_PARAMS, type);
        bundle.putInt(EXTRA_RANKINGLIST_SITE, site);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRequestParams = new BookListReqParams();
        Bundle arguments = getArguments();
        if (arguments != null) {
            mType = arguments.getString(EXTRA_RANKINGLIST_PARAMS);
            mSite = arguments.getInt(EXTRA_RANKINGLIST_SITE, 1);
            ((BookListReqParams)mRequestParams).order_type = mType;
            ((BookListReqParams) mRequestParams).site = mSite + "";
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void updateViews() {
        super.updateViews();
        mPresenter.setBookView(this);
        mPresenter.getRankingList((BookListReqParams) mRequestParams);
    }



    @Override
    public void onItemClick(View view, int position) {
        if (position - mAdapter.getHeaderViewsCount()>= mList.size()) {
            return;
        }
        ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", mList.get(position - mAdapter.getHeaderViewsCount()).book_id);
    }

    @Override
    protected BaseQuickAdapter provideAdapter() {
        if (TYPE_RANKINGLIST_RENQI.equals(mType)) {
            mAdapter = new RankingRenqiAdapter(getActivity(), mList);
        } else if (TYPE_RANKINGLIST_COLLECT.equals(mType)) {
            mAdapter = new RankingCollectAdapter(getActivity(), mList);
        } else if (TYPE_RANKINGLIST_FINISH.equals(mType)) {
            mAdapter = new RankingFinishedAdapter(getActivity(), mList);
        }
        if (mAdapter != null) {
            mAdapter.setOnItemClickListener(this);
        }
        return mAdapter;
    }

    @Override
    protected void getContentAysnc(PageRequestParams params) {
        mPresenter.getRankingList((BookListReqParams) mRequestParams);
    }

    @Override
    public void onChangeSex(int site) {
        ((BookListReqParams)mRequestParams).page = 1;
        ((BookListReqParams)mRequestParams).site = site + "";
        mSite = site;
        getContentAysnc(mRequestParams);
    }
}
